<?php

namespace App\Console\Commands\College;

use App\Models\College\CollegeTrainingOrderModel;
use App\Models\College\CollegeTrainingOrderUserModel;
use App\Models\College\CollegeTrainingPlanModel;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class InitCollege261 extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'college:init-261';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更新商家2.6.1上线字段';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function handle()
    {
        // 商家确认时间
        \App\Models\College\CollegeTrainingOrderModel::query()->where('merchantConfirmTime', 0)->get()->each(function (
            $item
        ) {
            $t = \App\Models\College\CollegeTrainingOrderLogModel::query()
                                                                 ->where('orderId', $item->id)
                                                                 ->where('content', '商家确认培训信息')
                                                                 ->select('createdAt')
                                                                 ->first();
            if ($t) {
                $item->merchantConfirmTime = $t->createdAt->timestamp;
                $item->save();

                $this->info($item->id . ' - ' . $item->merchantConfirmTime);
            }


        });

        // 培训结果
        CollegeTrainingOrderModel::query()
                                 ->with('userRel')
                                 ->where('result', CollegeTrainingOrderModel::RESULT_INIT)
                                 ->where('status', CollegeTrainingOrderModel::STATUS_FINISH)
                                 ->get()
                                 ->each(function ($item) {

                                     $successNum = $item->userRel
                                         ->where('finalResult', CollegeTrainingOrderUserModel::RESULT_OK)->count();
                                     $totalNum   = $item->userRel->count();

                                     // 所有人均通过 理论&实操考试
                                     if ($successNum == $totalNum && $totalNum != 0) {
                                         $item->result = CollegeTrainingOrderModel::RESULT_SUCCESS;
                                     } else {
                                         $item->result = CollegeTrainingOrderModel::RESULT_FAIL;
                                     }

                                     $item->save();

                                     $this->info($item->id . ' - ' . $item->merchantConfirmTime);
                                 });

        Log::info("InitCollege261 finish");
        $this->info("InitCollege261 finish");
    }
}
